<?php

use app\models\Patientpower;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model app\models\Patient */
/* @var $form yii\widgets\ActiveForm */
?>
<?= $this->registerJsFile('@web/assets/layui/city-picker.js', ['position' => $this::POS_END]) ?>

<style type="text/css">
    .patient-create,
    .patient-update {
        background-color: #eee;
        padding: 1em;
    }

    label.required::after {
        content: " * ";
        color: red;
    }

    .disease_deparment_table td .layui-form-radio {
        min-width: 160px;
    }

    .layui-form-item {
        border-top: #ccc 1px solid;
        border-left: #ccc 1px solid;
        background-color: #fff;
    }

    .layui-form-pane .layui-form-label {
        margin: 0;
    }

    .layui-form-pane .layui-form-item.disabled input {
        background-color: #eee;
        border-bottom: #fff 1px solid;
    }

    .layui-form-pane .layui-form-item.button {
        background-color: #eee;
        text-align: center;
    }

    .layui-form-pane .layui-form-item.button button {
        margin-top: 2em;
        width: 50%;
        font-weight: 800;
    }

    #select_datetime_button td .layui-btn-xs {
        width: 50px;
        height: 28px;
        line-height: 28px;
        display: inline-block;
        border: #999 1px solid;
        border-radius: 5px;
        padding: 0 5px;
        text-align: center;
        cursor: pointer;
    }

    #patient_address .layui-select-default {
        margin-bottom: 0;
    }

    table.disease_deparment_table {
        width: 100%;
        border-top: 1px #eee solid;
        border-left: 1px #eee solid;
        border-radius: 0 2px 2px 0;
    }
</style>
<?php $errors = $model->getErrors();
if (Yii::$app->request->isPost && null != $errors && count($errors) > 0) { ?>
    <div style="clear:both;width:100%;margin:1em 0;position:relative;min-height:80px;">
        <?php foreach ($errors as $title => $e) {  ?>
            <div class="layui-card" style="width:24%;margin-right:1%;float:left;">
                <div class="layui-card-header">
                    <div style="width:100%;color:#FF5722"><i class="layui-layer-ico layui-layer-ico2" style="width:30px;height:30px;float:left;margin-top:5px;"></i>&nbsp;<?= $title ?></div>
                </div>
                <div class="layui-card-body">
                    <?php foreach ($e as $s) {  ?>
                        <?= $s ?><br />
                    <?php } ?>
                </div>
            </div>
        <?php } ?>
    </div>
<?php } ?>

<div class="patient-form" style="width: 100%;clear:both;">
    <form class="layui-form layui-form-pane" method="post" action="<?= Yii::$app->request->url ?>">
        <input name="_csrf" type="hidden" id="_csrf" value="<?= Yii::$app->request->csrfToken ?>">
        <div class="layui-fluid layui-col-space30">
            <div class="layui-col-lg6" style="margin-left: -15px;">
                <div class="layui-form-item">
                    <label class="layui-form-label required">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" id="patient_name" name="patient[name]" value="<?= $model->name ?>" required lay-verify="required" placeholder="请输入患者姓名" autocomplete="off" class="layui-input" onblur="patient_name_validate(this.value)" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="patient[sex]" value="男" title="男" <?= $model->sex == '男' ? 'checked' : '' ?> />
                        <input type="radio" name="patient[sex]" value="女" title="女" <?= $model->sex == '女' || $model->sex == null ? 'checked' : '' ?> />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label required">电话</label>
                    <div class="layui-input-block">
                        <input type="number" id="patient_telphone" name="patient[telphone]" value="<?= $model->telphone ?>" required lay-verify="required" placeholder="请输入患者电话" autocomplete="off" class="layui-input" onblur="patient_telphone_validate(this.value)" />
                    </div>
                </div>

                <?php if (Yii::$app->controller->action->id != 'record' && Yii::$app->controller->action->id != 'editrecord') { ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">是否现场</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="patient[istoday]" lay-skin="switch" lay-text="是|否" value="是" <?= $model->istoday == '是' ? 'checked' : '' ?> />
                        </div>
                    </div>
                <?php } ?>

                <div class="layui-form-item">
                    <label class="layui-form-label required">永久身份</label>
                    <div class="layui-input-block">
                        <input type="text" name="patient[forever_id]" value="<?= $model->forever_id ?>" required lay-verify="required" placeholder="请输入患者在商务通中的永久身份。若没找到，填写：“没找到，等待竞价填写”" autocomplete="off" class="layui-input" />
                    </div>
                </div>

                <?php if (Yii::$app->controller->action->id != 'record' && Yii::$app->controller->action->id != 'editrecord') { ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">预约时间</label>
                        <div class="layui-input-block">
                            <input id="order_date_time" type="datetime" value="<?= $model->order_time ?>" name="patient[order_time]" required lay-verify="required" placeholder="请输入患者预约时间" autocomplete="off" class="layui-input" />
                        </div>
                        <table id="select_datetime_button" style="margin:0 0.5em;width:100%;">
                            <tr>
                                <td style="padding:0.5em;border-bottom:#ddd 1px solid;width:100px;text-align:right;color:#ccc;">日期:</td>
                                <td style="padding:0.5em;border-bottom:#ddd 1px solid;">
                                    <div onclick="select_date('<?= date('Y-m-d') ?>')" class="layui-btn-xs">今天</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('+1 day')) ?>')" class="layui-btn-xs">明天</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('+2 day')) ?>')" class="layui-btn-xs">后天</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('+3 day')) ?>')" class="layui-btn-xs">大后天</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('next Saturday')) ?>')" class="layui-btn-xs">周六</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('next Sunday')) ?>')" class="layui-btn-xs">周日</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('next Monday')) ?>')" class="layui-btn-xs">周一</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('+7 day')) ?>')" class="layui-btn-xs">一周后</div>
                                    <div onclick="select_date('<?= date('Y-m-d', strtotime('+15 day')) ?>')" class="layui-btn-xs">半月后</div>
                                </td>
                            </tr>
                            <tr>
                                <td style="padding:0.5em;text-align:right;color:#ccc;">时间:</td>
                                <td style="padding:0.5em;">
                                    <div onclick="select_time('08:00:00')" class="layui-btn-xs">8点</div>
                                    <div onclick="select_time('09:00:00')" class="layui-btn-xs">9点</div>
                                    <div onclick="select_time('10:00:00')" class="layui-btn-xs">10点</div>
                                    <div onclick="select_time('11:00:00')" class="layui-btn-xs">11点</div>
                                    <div onclick="select_time('12:00:00')" class="layui-btn-xs">12点</div>
                                    <div onclick="select_time('13:00:00')" class="layui-btn-xs">13点</div>
                                    <div onclick="select_time('14:00:00')" class="layui-btn-xs">14点</div>
                                    <div onclick="select_time('15:00:00')" class="layui-btn-xs">15点</div>
                                    <div onclick="select_time('16:00:00')" class="layui-btn-xs">16点</div>
                                    <div onclick="select_time('17:00:00')" class="layui-btn-xs">17点</div>
                                    <div onclick="select_time('18:00:00')" class="layui-btn-xs">18点</div>
                                </td>
                            </tr>
                        </table>
                    </div>
                <?php }  ?>

            </div>
            <div class="layui-col-lg6">

                <div class="layui-form-item <?= null != $user_array && count($user_array) > 1 ? '' : 'disabled' ?>">
                    <label class="layui-form-label required">咨询人</label>
                    <div class="layui-input-block">
                        <?php if (null != $user_array && count($user_array) > 1) { ?>
                            <select name="patient[user_id]" lay-verify="required">
                                <?php foreach ($user_array as $d) { ?>
                                    <option value="<?= $d['id'] ?>" <?= $d['checked'] ? 'selected="selected"' : '' ?>><?= $d['name'] ?></option>
                                <?php } ?>
                            </select>
                        <?php } else { ?>
                            <input type="text" disabled value="<?= Yii::$app->user->identity->showname ?>" autocomplete="off" class="layui-input" />
                        <?php }  ?>
                    </div>
                </div>

                <?php if (Yii::$app->user->identity->jobModel->check_patient_power('修改班次') == '可修改' || Yii::$app->user->identity->jobModel->check_patient_power('修改班次') == 'super') { ?>
                    <div class="layui-form-item">
                        <label class="layui-form-label">班次</label>
                        <div class="layui-input-block">
                            <?php foreach ($workgroup_array as $d) { ?>
                                <input type="radio" name="patient[workgroup]" value="<?= $d['name'] ?>" title="<?= $d['name'] ?>" <?= $d['checked'] ? 'checked="checked"' : '' ?> />
                            <?php } ?>
                        </div>
                    </div>
                <?php } ?>

                <div class="layui-form-item">
                    <label class="layui-form-label">年龄</label>
                    <div class="layui-input-block">
                        <input type="number" name="patient[age]" value="<?= $model->age ?>" placeholder="请输入患者年龄" autocomplete="off" class="layui-input" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">预约号</label>
                    <div class="layui-input-block">
                        <input type="text" name="patient[order_number]" value="<?= $model->order_number ?>" placeholder="请输入预约号" autocomplete="off" class="layui-input" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">微信</label>
                    <div class="layui-input-block">
                        <input type="text" name="patient[wechat]" value="<?= $model->wechat ?>" placeholder="请输入患者微信" autocomplete="off" class="layui-input" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">QQ</label>
                    <div class="layui-input-block">
                        <input type="text" name="patient[qq]" value="<?= $model->qq ?>" placeholder="请输入患者QQ" autocomplete="off" class="layui-input" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">地址</label>
                    <div class="layui-input-block" id="patient_address">
                        <input type="hidden" id="patient_address_value" name="patient[address]" value="<?= $model->address ?>" placeholder="请输入患者地址" autocomplete="off" class="layui-input" />
                        <div class="layui-row" style="display:inline-table">
                            <div class="layui-col-sm4">
                                <div class="layui-inline layui-select-default" style="width: 99%;">
                                    <select id="p_province" name="province" data-area="<?= $address_array[0] ?>" lay-filter="province">
                                        <option value="">选择省</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-sm4">
                                <div class="layui-inline layui-select-default" style="width: 99%;">
                                    <select id="p_city" name="city" data-area="<?= $address_array[1] ?>" lay-filter="city">
                                        <option value="">选择市</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-col-sm4">
                                <div class="layui-inline layui-select-default" style="width: 99%;">
                                    <select id="p_county" name="district" data-area="<?= $address_array[2] ?>" lay-filter="district">
                                        <option value="">选择区</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">搜关键词</label>
                    <div class="layui-input-block">
                        <input type="text" name="patient[keyword]" value="<?= $model->keyword ?>" placeholder="请输入患者搜索的关键词" autocomplete="off" class="layui-input" />
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item input_border" style="display: none;">
            <label class="layui-form-label required">预约科室</label>
            <div class="layui-input-block">
                <?php foreach ($department_array as $d) { ?>
                    <input type="radio" name="patient[department_id]" value="<?= $d['id'] ?>" title="<?= $d['name'] ?>" <?= $d['checked'] ? 'checked="checked"' : '' ?> lay-filter="selectDepartment" />
                <?php } ?>
            </div>
        </div>
        <div class="layui-form-item input_border">
            <label class="layui-form-label required">预约疾病</label>
            <div class="layui-input-block">
                <?php foreach ($department_array as $d) { ?>
                    <table class="disease_deparment_table" id="disease_table_<?= $d['id'] ?>" style="<?= $d['checked'] ? '' : 'display:none;' ?>">
                        <?php foreach ($d['disease_array'] as $root) { ?>
                            <tr style="border-bottom:1px #eee solid;">
                                <td style="border-right: #eee 1px solid;text-align:center;" width="50">
                                    <label><?= $root['name'] ?></label>
                                </td>
                                <td style="border-right: #ccc 1px solid;" width="600" id="disease_root_<?= $root['id'] ?>_children">
                                    <?php foreach ($root['children'] as $d) { ?>
                                        <input type="radio" name="patient[disease_ids]" value="<?= $d['id'] ?>" title="<?= $d['name'] ?>" <?= $d['checked'] ? 'checked="checked"' : '' ?> />
                                    <?php } ?>
                                </td>
                            </tr>
                        <?php } ?>
                    </table>
                <?php } ?>
            </div>
        </div>

        <div class="layui-form-item input_border">
            <label class="layui-form-label required">来源渠道</label>
            <div class="layui-input-block">
                <?php foreach ($comeway_array as $d) { ?>
                    <input type="radio" name="patient[comeway_id]" value="<?= $d['id'] ?>" title="<?= $d['name'] ?>" <?= $d['checked'] ? 'checked="checked"' : '' ?> />
                <?php } ?>
            </div>
        </div>

        <div class="layui-form-item input_border">
            <label class="layui-form-label required">咨询方式</label>
            <div class="layui-input-block">
                <?php foreach ($communication_array as $d) { ?>
                    <input type="radio" name="patient[communication_id]" value="<?= $d['id'] ?>" title="<?= $d['name'] ?>" <?= $d['checked'] ? 'checked="checked"' : '' ?> />
                <?php } ?>
            </div>
        </div>

        <div class="layui-form-item input_border">
            <label class="layui-form-label">预约医生</label>
            <div class="layui-input-block">
                <?php foreach ($department_array as $d) { ?>
                    <div class="doctor_deparment_div" id="doctor_div_<?= $d['id'] ?>" style="<?= $d['checked'] ? '' : 'display:none;' ?>">
                        <?php foreach ($d['doctor_array'] as $d) { ?>
                            <input type="radio" name="patient[order_doctor_id]" value="<?= $d['id'] ?>" title="<?= $d['name'] ?>" <?= $d['checked'] ? 'checked="checked"' : '' ?> />
                        <?php } ?>
                    </div>
                <?php } ?>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">病情备注</label>
            <div class="layui-input-block">
                <textarea name="patient[condition_log]" placeholder="请输入内容" class="layui-textarea"><?= $model->condition_log ?></textarea>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">聊天记录</label>
            <div class="layui-input-block">
                <textarea id="talk_edit_textarea" name="patient[talk_log]" placeholder="请输入内容" class="layui-textarea"><?= $model->talk_log ?></textarea>
            </div>
        </div>
        <div class="layui-form-item button">
            <button class="layui-btn" lay-submit lay-filter="formBeforeSubmit">
                立 即 创 建
            </button>
        </div>
    </form>

</div>
<script>
    function select_date(value_date) {
        $('#order_date_time').val(value_date + ' 00:00:00');
    }

    function select_time(value_time) {
        var selected_date = $('#order_date_time').val();
        if (selected_date.length < 10) {
            layer.alert('先选择日期，才能选择时间', {
                icon: 2
            });
            return false;
        }
        $('#order_date_time').val(selected_date.slice(0, 10) + ' ' + value_time);
    }

    function layui_render() {
        var now_time = new Date();
        layui.laydate.render({
            elem: '#order_date_time',
            type: 'datetime',
            min: 'now_time'
        });
        layui.layedit.build('talk_edit_textarea', {
            tool: ['strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right'],
        });

        layui.config({
            base: '/assets/layui/' //假设这是你存放拓展模块的根目录
        }).extend({ //设定模块别名
            common: 'common' //如果 common.js 是在根目录，也可以不用设定别名
        });

        layui.use(['form', 'common'], function() {
            var common = layui.common,
                form = layui.form;
            //do something...
            //三级地址联动
            common.showCity('province', 'city', 'district');

            form.on('radio(selectDepartment)', function(data) {
                $('input[name="patient[disease_ids]"]').prop('checked', false);
                $('input[name="patient[order_doctor_id]"]').prop('checked', false);
                form.render('radio');
                $('.disease_deparment_table').hide();
                $('.doctor_deparment_div').hide();
                return (function() {
                    $('#disease_table_' + data.value).show();
                    $('#doctor_div_' + data.value).show();
                })();
            });

            //监听提交
            form.on('submit(formBeforeSubmit)', function(data) {
                var resData = data.field,
                    province = resData.province,
                    city = resData.city,
                    district = resData.district;

                // 通过地址code码获取地址名称
                var address = common.getCity({
                    province,
                    city,
                    district
                });

                resData['patient[address]'] = address.provinceName + '-' + address.cityName + '-' + address.districtName;
                $('#patient_address_value').val(resData['patient[address]']);

                var validate_result = true;
                if (undefined == resData['patient[department_id]'] || '' == resData['patient[department_id]']) {
                    layer.alert('预约科室是必选项', {
                        icon: 2,
                        shadeClose: true
                    });
                    validate_result = false;
                }
                if (undefined == resData['patient[comeway_id]'] || '' == resData['patient[comeway_id]']) {
                    layer.alert('来源渠道是必选项', {
                        icon: 2,
                        shadeClose: true
                    });
                    validate_result = false;
                }
                if (undefined == resData['patient[communication_id]'] || '' == resData['patient[communication_id]']) {
                    layer.alert('咨询方式是必选项', {
                        icon: 2,
                        shadeClose: true
                    });
                    validate_result = false;
                }

                return validate_result;
            });

        });
    }
    window.setTimeout("layui_render()", 500);

    function patient_name_validate(name) {
        $.getJSON("<?= Url::toRoute('patient/validatename') . '&_csrf=' . Yii::$app->request->csrfToken ?>&name=" + name, function(res) {
            var warn_string = "";
            if (null != res.data && res.data.length > 0) {
                for (var index in res.data) {
                    warn_string += res.data[index].username + '在' + res.data[index].createtime + '录入过【' + name + '】、预约时间为：' + res.data[index].order_time + '、到诊时间为：' + res.data[index].arrive_time + '<br />';
                }
            }
            if (warn_string != "") {
                layer.alert(warn_string, {
                    icon: 2
                });
            }
        });
    }

    function patient_telphone_validate(number) {
        if (number.length > 1) {
            $.getJSON("<?= Url::toRoute('patient/validatetelphone') . '&_csrf=' . Yii::$app->request->csrfToken ?>&number=" + number, function(res) {
                var warn_string = "";
                if (null != res.data && res.data.length > 0) {
                    for (var index in res.data) {
                        warn_string += res.data[index].username + ' 在' + res.data[index].createtime + '录入过【' + number + '】、预约时间为：' + res.data[index].order_time + '、到诊时间为：' + res.data[index].arrive_time + '<br />';
                    }
                }
                if (warn_string != "") {
                    layer.alert(warn_string, {
                        icon: 2
                    });
                }
            });
        }
    }
</script>